package com.jnzc.ocr.serv.shop.api.impl;

import com.jnzc.ocr.common.bean.PageResult;
import com.jnzc.ocr.serv.shop.api.ShopProductApi;
import com.jnzc.ocr.serv.shop.entity.ShopProduct;
import com.jnzc.ocr.serv.shop.entity.ShopProductSku;
import com.jnzc.ocr.serv.shop.filter.ShopProductFilter;
import com.jnzc.ocr.serv.shop.service.ShopProductService;
import com.jnzc.ocr.serv.shop.service.ShopProductSkuService;
import com.jnzc.ocr.serv.shop.vo.ShopProductInfoVo;
import com.jnzc.ocr.serv.shop.vo.ShopProductSaveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhangcm
 * @version 1.0, 2017-09-29
 */
@RestController
public class ShopProductApiImpl implements ShopProductApi {

    @Autowired
    private ShopProductService shopProductService;

    @Autowired
    private ShopProductSkuService shopProductSkuService;

    @Override
    public ShopProduct find(Long id, Long shopId) {
        return shopProductService.find(id, shopId);
    }

    @Override
    public ShopProductInfoVo findInfo(Long id, Long shopId) {
        return shopProductService.findInfo(id, shopId);
    }

    @Override
    public PageResult<ShopProduct> searchWithSku(ShopProductFilter filter, Integer pageNo, Integer pageSize) {
        PageResult<ShopProduct> pageResult = shopProductService.search(filter, pageNo, pageSize);
        for (ShopProduct product : pageResult.getList()) {
            List<ShopProductSku> skuList = shopProductSkuService.list(product.getId(), product.getShopId());
            product.setSkuList(skuList);
        }
        return pageResult;
    }

    @Override
    public void save(@RequestBody ShopProductSaveVo param, Long shopId) {
        shopProductService.save(param, shopId);
    }

    @Override
    public void delete(Long id, Long shopId) {
        shopProductService.delete(id, shopId);
    }

    @Override
    public void clearLowStock(Long shopId) {
        shopProductService.clearLowStock(shopId);
    }

    @Override
    public void updateLowStock(@RequestBody List<Long> idList, Long shopId) {
        shopProductService.updateLowStock(idList, shopId);
    }

    @Override
    public void updateSale(Long id, Long shopId, Boolean ifOnSale) {
        shopProductService.updateSale(id, shopId, ifOnSale);
    }
}
